package com.xiaoqi.ajax.service;

import com.google.gson.Gson;
import com.xiaoqi.ajax.domain.Area;
import com.xiaoqi.ajax.domain.City;
import com.xiaoqi.ajax.domain.Province;
import com.xiaoqi.ajax.util.DBCPUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author Rookie Coder
 * @Description
 * @Date
 **/
@WebServlet("/selectPAC")
public class AjaxSlectPCA extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if (action==null || "".equals(action)){
            return;
        }
        if ("province".equals(action)){
            selectProvince(req,resp);
        }
        if ("city".equals(action)){
            selectCity(req,resp);
        }
        if ("area".equals(action)){
            selectArea(req,resp);
        }
    }
    private void selectArea(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        String cid = req.getParameter("cid");
        System.out.println("cid====================================="+cid);
        String sql = "select areaID,areas,father from areas where father = ?";
        DBCPUtil dbcpUtil = new DBCPUtil();
        List<Area> query = dbcpUtil.query(sql, Area.class, cid);
        System.out.println("-----------------------");
        System.out.println(query);
        System.out.println("-----------------------");
        Gson gson = new Gson();
        String json = gson.toJson(query);
        writer.println(json);
        writer.flush();
        writer.close();

    }
    private void selectCity(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String provinceID = req.getParameter("provinceID");
        resp.setContentType("text/xml;charset=utf-8");
        String sql = "select cityID,city,father from city where father=?";
        DBCPUtil dbcpUtil =new DBCPUtil();
        PrintWriter writer = resp.getWriter();
        List<City> query = dbcpUtil.query(sql, City.class, provinceID);
        StringBuffer sb = new StringBuffer();
        sb.append("<Citys>");
        for (City c: query) {
            sb.append("<city>");
            sb.append("<id>"+c.getCityID()+"</id>");
            sb.append("<name>"+c.getCity()+"</name>");
            sb.append("</city>");
        }
        sb.append("</Citys>");
        writer.write(sb.toString());
        writer.flush();
        writer.close();

    }
    private void selectProvince(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String sql = "select provinceID,province from province";
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        DBCPUtil dbcpUtil =new DBCPUtil();
        List<Province> query = dbcpUtil.query(sql, Province.class);
        // 封装list
        StringBuffer sb = new StringBuffer();
        sb.append("-1,--请选择省份--");
        for (Province p:query) {
            sb.append(";");
            sb.append(p.getProvinceID());
            sb.append(",");
            sb.append(p.getProvince());
        }
        writer.println(sb);
        writer.flush();
        writer.close();
    }
}
